﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using G15_Agent.Models;
using System.Data;

namespace G15_Agent.Controllers
{
    public class HoaDonController : Controller
    {
        private AgentContext DatabaseSQL = new AgentContext();
        //
        // GET: /HoaDon/

        public ActionResult HoaDonKH()
        {
            List<HoaDonKH> ListHoaDonKH = DatabaseSQL.HoaDonKHs.ToList();
            return View(ListHoaDonKH);
        }

        public ActionResult HoaDonNCC()
        {
            List<HoaDonNCC> ListHoaDonNCC = DatabaseSQL.HoaDonNCCs.ToList();
            return View(ListHoaDonNCC);
        }
        
        public ActionResult Detail(ChiTietHDKH model, string id)
        {
            List<ChiTietHDKH> ListChiTiet = DatabaseSQL.ChiTietHDKHs.Where(d => d.MaHD_KH == id ).ToList();
                return View(ListChiTiet);
        }

        public ActionResult DetailNCC(ChiTietHDKH model, string id)
        {
            List<ChiTietHDKH> ListChiTiet = DatabaseSQL.ChiTietHDKHs.Where(d => d.MaHD_NCC == id ).ToList();
            return View(ListChiTiet);
        }

        public ActionResult HoaDonMayBay()
        {
            List<ChiTietHDKH> HoaDon = DatabaseSQL.ChiTietHDKHs.Where(d => d.HoaDonNCC.DanhSachNCC.LoaiNCC == 1 ).ToList();
            ViewBag.NCC = new SelectList(DatabaseSQL.DanhSachNCCs, "MaNCC", "TenNCC");
            return View(HoaDon);
        }

        public ActionResult HoaDonDatPhong()
        {
            List<ChiTietHDKH> HoaDon = DatabaseSQL.ChiTietHDKHs.Where(d => d.HoaDonNCC.DanhSachNCC.LoaiNCC == 2 ).ToList();
            return View(HoaDon);
        }

        public ActionResult HoaDonThueXe()
        {
            List<ChiTietHDKH> HoaDon = DatabaseSQL.ChiTietHDKHs.Where(d => d.HoaDonNCC.DanhSachNCC.LoaiNCC == 3 ).ToList();
            return View(HoaDon);
        }

        public ActionResult HoaDonMuaTour(ChiTietHDKH model)
        {
            List<ChiTietHDKH> HoaDon = DatabaseSQL.ChiTietHDKHs.Where(d => d.HoaDonNCC.DanhSachNCC.LoaiNCC == 4 ).ToList();
            return View(HoaDon);
        }

        public ActionResult createChiTietHD()
        {
            return View();
        }

        [HttpPost]
        public ActionResult CreateChiTietHD(ChiTietHDKH model)
        {
            if (ModelState.IsValid)
            {

                DatabaseSQL.ChiTietHDKHs.Add(model);
                DatabaseSQL.SaveChanges();

                TempData["message"] = string.Format("{0} Đã được tạo", model.MaChiTietHD);
                return RedirectToAction("Detail", new { id = model.MaHD_KH});
            }
            
            return View(model);
        }

        //Kich hoa hoa don khach hang
        public ActionResult ActiveOderUser(string id)
        {
            HoaDonKH model = DatabaseSQL.HoaDonKHs.Find(id);

            return View(model);
        }

        //


        [HttpPost, ActionName("ActiveOderUser")]
        public ActionResult KichHoatHDKH(string id)
        {
            HoaDonKH model = DatabaseSQL.HoaDonKHs.Find(id);
            model.TrangThai = 1;
            DatabaseSQL.SaveChanges();
            return RedirectToAction("HoaDonKH");
        }

        //Vo hieu hoa don khach hàng
        public ActionResult InactiveOderUser(string id)
        {
            HoaDonKH model = DatabaseSQL.HoaDonKHs.Find(id);

            return View(model);
        }

       
        [HttpPost, ActionName("InactiveOderUser")]
        public ActionResult VoHieuHDKH(string id)
        {
            HoaDonKH model = DatabaseSQL.HoaDonKHs.Find(id);
            model.TrangThai = 0;
            DatabaseSQL.SaveChanges();
            return RedirectToAction("HoaDonKH");
        }

        // Kích hoạt hóa đơn nhà cung cấp
        public ActionResult ActiveOderService(string id)
        {
            HoaDonNCC model = DatabaseSQL.HoaDonNCCs.Find(id);

            return View(model);
        }

        //


        [HttpPost, ActionName("ActiveOderService")]
        public ActionResult KichHoatHDNCC(string id)
        {
            HoaDonNCC model = DatabaseSQL.HoaDonNCCs.Find(id);
            model.TrangThai = 1;
            DatabaseSQL.SaveChanges();
            return RedirectToAction("HoaDonNCC");
        }

        // vô hiệu hóa đơn nhà cung cấp
        public ActionResult InactiveOderService(string id)
        {
            HoaDonNCC model = DatabaseSQL.HoaDonNCCs.Find(id);

            return View(model);
        }

        //


        [HttpPost, ActionName("InactiveOderService")]
        public ActionResult VoHieuHDNCC(string id)
        {
            HoaDonNCC model = DatabaseSQL.HoaDonNCCs.Find(id);
            model.TrangThai = 0;
            DatabaseSQL.SaveChanges();
            return RedirectToAction("HoaDonNCC");
        }

        // Kích hoạt chi tiết hóa đơn KH
        public ActionResult ActiveCTKH(string id)
        {
            ChiTietHDKH model = DatabaseSQL.ChiTietHDKHs.Find(id);

            return View(model);
        }

        //


        [HttpPost, ActionName("ActiveCTKH")]
        public ActionResult KichHoatCT(string id, int i)
        {
            ChiTietHDKH model = DatabaseSQL.ChiTietHDKHs.Find(id);
            model.TrangThai = 1;
            DatabaseSQL.SaveChanges();

            if (i == 0)
                return RedirectToAction("Detail", new { id = model.MaHD_KH});
            if (i == 1)
                return RedirectToAction("HoaDonMayBay");
            if (i == 2)
                return RedirectToAction("HoaDonDatPhong");
            if (i == 3)
                return RedirectToAction("HoaDonThueXe");
            if (i == 4)
                return RedirectToAction("HoaDonMuaTour");
            else
                return RedirectToAction("DetailNCC", new { id = model.MaHD_NCC});
            
        }

        // vô hiệu chi tiết hóa đơn KH
        public ActionResult InactiveCTKH(string id)
        {
            ChiTietHDKH model = DatabaseSQL.ChiTietHDKHs.Find(id);

            return View(model);
        }

        //


        [HttpPost, ActionName("InactiveCTKH")]
        public ActionResult VoHieuCT(string id, int i)
        {
            ChiTietHDKH model = DatabaseSQL.ChiTietHDKHs.Find(id);
            model.TrangThai = 0;
            DatabaseSQL.SaveChanges();
            if (i == 0)
                return RedirectToAction("Detail", new { id = model.MaHD_KH });
            if (i == 1)
                return RedirectToAction("HoaDonMayBay");
            if (i == 2)
                return RedirectToAction("HoaDonDatPhong");
            if (i == 3)
                return RedirectToAction("HoaDonThueXe");
            else
                return RedirectToAction("HoaDonMuaTour");
            
        }
    }
}
